// 进度条加载提示
NProgress.start();
window.onload = function () {
    NProgress.done();
}
// layui相关
layui.use(['element', 'table', 'form', 'jquery', 'laydate', 'util'], function () {
    let element = layui.element;
    let table = layui.table;
    let form = layui.form;
    let $ = layui.jquery;
    //封面图面
    let photoTpl = `<script type="text/html" id="photoTpl"><img src="{{d.photo}}"</script> `;
    //工具条
    let operationTpl = `<script type="text/html" id="operationTpl"><a href="javascript:;" title="编辑" lay-event="edit"><i class="layui-icon">&#xe642;</i></a><a href="javascript:;" title="删除" lay-event="del"><i class="layui-icon">&#xe640;</i></a></script>`;
    //渲染数据表格
    table.render({
        elem: '#tableId',
        url: '/manage/students/',
        limit: 10,
        page: true,
        even: true,
        cols: [[
            {type: 'checkbox'},
            {field: 'sid', title: 'ID', width: 80},
            {field: 'sname', title: '学生姓名', width: 350},
            {field: 'photo', title: '照片', templet: photoTpl},
            {field: 'sort', title: '排序', width: 90, edit: 'text'},
            {title: '操作', fixed: 'right', minWidth: 80, templet: operationTpl, align: 'center'}
        ]],
        done: function (res, curr, count) {
            $("#countNum").text(count);
        }
    });

    //查询事件
    // form.on('submit(search)', function () {
    //     layer.msg('正在查询，请稍后...');
    //     return false;
    // });

    //表格工具列
    table.on('tool(tableFilter)', function (obj) {
        // var data = obj.data;
        let layEvent = obj.event;
        if (layEvent === 'edit') {
            var editLayer = layer.open({
                title: '编辑就业信息',
                type: 2,
                maxmin: true,
                shade: 0.5,
                area: ['100%', '100%'],
                content: '/manage/students/edit?sid=' + obj.data.sid,
                zIndex: layer.zIndex,
                ready: function () {
                    //最大化页面层
                    layer.full();
                },
                end: function () {
                    $(".layui-laypage-btn")[0].click();
                }
            });
        } else if (layEvent === 'del') {
            layer.confirm("确定要删除吗？", {skin: 'layui-layer-lan', icon: 2, title: '提示', anim: 6}, function () {
                $.ajax({
                    url: '/manage/students/' + obj.data.sid,
                    type: 'DELETE',
                    success: function (resp) {
                        layer.msg("操作成功！", {icon: 1, time: 1000});
                        $(".layui-laypage-btn")[0].click();
                    },
                    error: function () {
                        layer.msg("操作失败！", {icon: 1, time: 1000});
                    }
                })
            });
        }
    });
    //添加文章
    $("#addUser").click(function () {
        var addLayer = layer.open({
            title: '添加就业信息',
            type: 2,
            maxmin: true,
            shade: 0.5,
            anim: 4,
            area: ['90%', '95%'],
            content: '/manage/students/add.html',
            zIndex: layer.zIndex,
            ready: function () {
                layer.full();
            },
            end: function () {
                $(".layui-laypage-btn")[0].click();
            }
        });
    });
    //修改排序
    table.on('edit(tableFilter)', function (obj) {
        let data = {sid: obj.data.sid, sort: obj.value};
        $.ajax({
            url: '/manage/students/',
            data: JSON.stringify(data),
            contentType: 'application/json',
            type: 'PUT',
            // dataType: 'json',
            success: function () {
                layer.msg("操作成功！", {icon: 6, time: 1000, anim: 4});
            }
            , error: function () {
                layer.msg("修改失败!", {icon: 5, time: 1000, anim: 4})
            }
        });

    });

});